<cfcomponent>
	<cffunction name="getparent" access="public" returntype="query" hint="returns information about the referring member">
		<cfargument name="parentid" type="string" required="yes" hint="account id of the referring member (should be stored in a cookie)">

            <cfquery name = "qParent" datasource="#request.dsn#" maxrows="1">
            SELECT * FROM core_accounts
            WHERE core_accounts.aid = <cfqueryparam value="#parentid#" cfsqltype="cf_sql_integer">
            </cfquery>
	
		<cfreturn qParent>
	</cffunction>
	<cffunction name="insertreferral" access="public" returntype="query" hint="Logs a referral into the core_referrals table in case the cookie is lost.">
		<cfargument name="refid" type="numeric" required="yes" hint="account id of the referring member (should be stored in a cookie)">
        <cfargument name="ipaddress" type="string" required="yes" hint="ip address of the visitor">
			
			<cfset refdate = now()>
            <cfquery name = "qinsertreferral" datasource="#request.dsn#">
			INSERT INTO core_referrals
            (aid, ip, dt)
            VALUES
            (#refid#, '#ipaddress#', #createodbcdatetime(refdate)#)
            </cfquery>
	
    	<cfset success = 1>
		<cfreturn success>
	</cffunction>
	
    <cffunction name="getusers" access="public" returntype="query" hint="queries database for all user accounts matching inputs"> 
		<cfargument name="start" type="numeric" default="1" required="no" hint="sql start row">
		<cfargument name="disp" type="numeric" default="50" required="no" hint="max sql records to display">
		<cfargument name="sortby" type="string" default="joindate" required="no" hint="column to sort on">
        <cfargument name="sortorder" type="string" default="DESC" required="no" hint="ASC or DESC - sort order">
        <cfargument name="viewtype" type="numeric" default="0" required="no" hint="Type of account">
        <cfargument name="searchbox" type="string" default="" required="no" hint="search string">
	
		<cfset startsql = start - 1>
        <cfquery name = "qaccounts" datasource = "#request.dsn#">
        SELECT * FROM core_accounts 
        <cfif NOT len(searchbox) IS 0>
            WHERE core_accounts.lastname LIKE '%#searchbox#%' AND core_accounts.atype = '#viewtype#' OR core_accounts.firstname LIKE '%#searchbox#%' AND atype = '#viewtype#' LIKE '%#searchbox#%' OR core_accounts.aid LIKE '%#searchbox#%' AND core_accounts.atype = '#viewtype#'
        <cfelse>
        <cfif NOT viewtype IS 0>WHERE core_accounts.atype = '#viewtype#'</cfif>
        </cfif>
        ORDER BY #sortby# #sortorder#
        LIMIT #startsql#, #disp#
        </cfquery>
		
		<cfreturn qaccounts>
	</cffunction>

    <cffunction name="getusercount" access="public" returntype="numeric" hint="queries database and gets user count"> 
		<cfargument name="start" type="numeric" default="1" required="no" hint="sql start row">
		<cfargument name="disp" type="numeric" default="50" required="no" hint="max sql records to display">
		<cfargument name="sortby" type="string" default="joindate" required="no" hint="column to sort on">
        <cfargument name="sortorder" type="string" default="DESC" required="no" hint="ASC or DESC - sort order">
        <cfargument name="viewtype" type="numeric" default="0" required="no" hint="Type of account">
        <cfargument name="searchbox" type="string" default="" required="no" hint="search string">
	
		<cfset startsql = start - 1>
        <cfquery name = "qcount" datasource = "#request.dsn#">
		SELECT count(*) AS totalrecords FROM core_accounts  
        <cfif NOT len(searchbox) IS 0>
            WHERE core_accounts.lastname LIKE '%#searchbox#%' AND core_accounts.atype = '#viewtype#' OR core_accounts.firstname LIKE '%#searchbox#%' AND atype = '#viewtype#' LIKE '%#searchbox#%' OR core_accounts.aid LIKE '%#searchbox#%' AND core_accounts.atype = '#viewtype#'
        <cfelse>
        <cfif NOT viewtype IS 0>WHERE core_accounts.atype = '#viewtype#'</cfif>
        </cfif>
        ORDER BY #sortby# #sortorder#
        LIMIT #startsql#, #disp#
        </cfquery>
		
		<cfreturn qcount.totalrecords>
	</cffunction>

	<cffunction name="getuser" access="public" returntype="query" hint="queries a user account based on the record id in the database"> 
        <cfargument name="id" required="yes" type="numeric" hint="the record id of the user">

        <cfquery name = "quser" datasource = "#request.dsn#">
        SELECT * FROM core_accounts
		WHERE core_accounts.id = <cfqueryparam value="#id#" cfsqltype="cf_sql_integer">
        </cfquery>
		
		<cfreturn quser>
	</cffunction>

	<cffunction name="getuserbyaid" access="public" returntype="query" hint="queries a user account based on the aid"> 
        <cfargument name="aid" required="yes" type="numeric" hint="the record id of the user">

        <cfquery name = "quser" datasource = "#request.dsn#">
        SELECT * FROM core_accounts
		WHERE core_accounts.aid = <cfqueryparam value="#aid#" cfsqltype="cf_sql_integer">
        </cfquery>
		
		<cfreturn quser>
	</cffunction>

	<cffunction name="gettypes" access="public" returntype="query" hint="get the types of users"> 

        <cfquery name = "qtypes" datasource="#request.dsn#">
        SELECT * FROM core_accounttypes
        </cfquery>
        
        <cfreturn qtypes>    
	</cffunction>
    
	<cffunction name="genaid" access="public" returntype="string" hint="Creates a random account ID"> 
		<cfset charlist  = "0|2|4|6|8|9|7|5|3|1|0|2|4|6|8|9|7|5|3|1">
        <cfset afid = "">
        <cfloop condition="NOT len(afid)">
            <cfloop from="1" to="7" index="i">
                <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>
                <cfset ThisChar = ListGetAt(Charlist, ThisNum, "|")>
                <cfset afid = "#afid##ThisChar#">
            </cfloop>
            <cfquery name="Checkafid" datasource="#Request.dsn#">
            SELECT * FROM core_accounts
            WHERE aid = <cfqueryparam value="#afid#" cfsqltype="cf_sql_integer">
            </cfquery>
            <cfif Checkafid.RecordCount>
                <cfset afid = "">
            </cfif>
        </cfloop>
		<cfreturn afid>
     </cffunction>            

	<cffunction name="genvstring" access="public" returntype="string" hint="Creates a random verfication string"> 
    	<cfset charlist  = "0|2|4|6|8|9|7|5|3|1|0|2|4|6|8|9|7|5|3|1">
		<cfset today = now()>
		<cfset prevstring1 = dateformat(today, "yyyymmdd")>
        <cfset prevstring2 = timeformat(today, "hhmmss")>
        <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>
        <cfset RandChar1 = ListGetAt(Charlist, ThisNum, "|")>
        <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>
        <cfset RandChar2 = ListGetAt(Charlist, ThisNum, "|")>
        <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>
        <cfset RandChar3 = ListGetAt(Charlist, ThisNum, "|")>
        <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>
        <cfset RandChar4 = ListGetAt(Charlist, ThisNum, "|")>
        <cfset vstring = "#prevstring1##prevstring2##randchar1##randchar2##randchar3##randchar4#">
		<cfreturn vstring>
	</cffunction>
</cfcomponent>